<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/9
 * Time: 14:25
 */

namespace app\admin\controller;


use app\admin\model\LabelModel;
use app\admin\model\LawModel;
use cmf\controller\AdminBaseController;
use think\Db;

/**
 * Class LawController
 * @package app\admin\controller
 * 法律知识模块
 */
class LawController extends AdminBaseController
{
    protected $arrWhere = [];

    protected function _initialize()
    {
        $label_list = Db::name('label')->select();   //全部标签
        $label_recommend = Db::name('label')->where('recommend',2)->select();   //推荐标签
        $label_recommend_no = Db::name('label')->where('recommend',1)->select();   //没有推荐的标签

        $this->assign('label_list',$label_list);
        $this->assign('label_recommend',$label_recommend);
        $this->assign('label_recommend_no',$label_recommend_no);
    }

    /**
     * @return \think\response\View
     * 法律知识列表
     */
    public function law_list()
    {
        $label = request()->param('label');
        $create_time = request()->param('create_time');
        $end_time = request()->param('end_time');
        //判断筛选条件
        if(!empty($label)){
            $this->arrWhere['laws'] = ['like','%'.$label.'%'];
        }
        if(!empty($create_time)&&!empty($end_time)){
            $this->arrWhere['update_time'] = ['between time',[strtotime($create_time),strtotime($end_time)]];
        }
        //查询数据并做标签替换
        $list = Db::name('law')->where($this->arrWhere)->order('update_time desc')->paginate(8)->each(function ($item,$key){
            $array = explode(',',$item['laws']);
            $names_array = Db::name('label')->where('id','in',$array)->column('label_name','id');
            $item['laws'] = implode(",", $names_array);

            return $item;
        });
        //追加分页参数
        $list->appends(['label'=>$label,'create_time'=>$create_time,'end_time'=>$end_time]);

        $page = $list->render();
        return view('',['list'=>$list,'page'=>$page,'label'=>$label,'create_time'=>$create_time,'end_time'=>$end_time]);

    }


    /**
     * @return \think\response\View
     * 法律知识添加模块
     */
    public function add()
    {
        if(request()->isPost()){
            $data = request()->param();
            $data['laws'] = implode(',',$data['laws']);
            $data['content'] = htmlspecialchars_decode($data['content']);
            $team_model = new LawModel();
            $result = $team_model->allowField(true)->save($data);
            if($result){
                $this->success('添加成功!', url('law/law_list'));
            }else{
                $this->error('添加失败！',url('law/add'));
            }
        }

        return view();
    }

    /**
     * @return \think\response\View
     * 法律知识修改
     */
    public function law_edit()
    {
        $id = request()->param('id');//获取要修改的知识id
        $info = Db::name('law')->where('id',$id)->find();
        if(request()->post()){
            $data = request()->param();
            $data['laws'] = implode(',',$data['laws']);
            $data['content'] = htmlspecialchars_decode($data['content']);
            $law_model = new LawModel();
            $result = $law_model->allowField(true)->isUpdate(true)->data($data, true)->save();
            if($result){
                $this->success('修改成功!', url('law/law_list'));
            }else{
                $this->error('修改失败！',url('law/law_edit'));
            }
        }

        return view('',['info'=>$info]);
    }


    /**
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * 法律知识上下线
     */
    public function up_down()
    {
        $data = request()->param();
        $law_model = new LawModel();
        $result = $law_model->allowField(true)->isUpdate(true)->data($data, true)->save();
        if($result){
            echo 1;
        }else{
            echo 0;
        }
    }


    /**
     * 法律知识删除
     */
    public function delete()
    {
        $id = $this->request->param('id', 0, 'intval');
        if (Db::name('law')->delete($id) !== false) {
            $this->success("删除成功！");
        } else {
            $this->error("删除失败！");
        }
    }


    /**
     * @return \think\response\View
     * 法律知识-标签管理
     */
    public function label()
    {

        return view();
    }

    /**
     * 标签添加
     */
    public function label_add()
    {
        $label_name = request()->param('label_name');
        $thumbnail = request()->param('thumbnail');
        if(empty($label_name)){
            $this->error("请填写标签名!");
        }
        if(empty($thumbnail)){
            $this->error("请添加标签图!");
        }
        $label = new LabelModel();
        $status = $label->where('label_name',$label_name)->find();
        if(!empty($status)){
            $this->error("已有标签请勿重复添加！");
        }else{
            $result = $label->allowField(true)->save(['label_name'=>$label_name,'thumbnail'=>$thumbnail]);
            if($result){
                $this->success('添加成功!');
            }else{
                $this->error('添加失败！');
            }
        }
    }

    /**
     * 标签删除
     */
    public function label_del()
    {
        $label_id = request()->param('id');   //获取标签id
        $list = Db::query("SELECT * FROM cmf_law where position('$label_id' in laws)");
        //判断是否有知识文章包含该标签
        if(!empty($list)){
            $this->error('知识文章中有该标签请勿删除!');
        }
        if (Db::name('label')->delete($label_id) !== false) {
            $this->success("删除成功！");
        } else {
            $this->error("删除失败！");
        }
    }

    /**
     *标签推荐/不推荐
     * $recommend   int    1取消推荐|2为设置推荐
     */
    public function label_recommend()
    {
        $data = request()->param();
        $law_model = new LabelModel();
        $result = $law_model->allowField(true)->isUpdate(true)->data($data, true)->save();
        if($result){
            echo 1;
        }else{
            echo 0;
        }
    }
}